package com.yunquan.ohana.ui.calendar.lists.add

import android.content.Intent
import android.os.Bundle
import android.view.inputmethod.EditorInfo
import androidx.core.content.ContextCompat
import androidx.core.view.WindowInsetsControllerCompat
import com.google.gson.Gson
import com.yunquan.ohana.BR
import com.yunquan.ohana.R
import com.yunquan.ohana.base.ui.BaseActivity
import com.yunquan.ohana.databinding.ActivityListsAddBinding
import com.yunquan.ohana.entity.ListsEntity
import com.yunquan.ohana.utils.StringUtils
import com.yunquan.ohana.utils.Utils
import java.util.Locale

class ListsAddActivity : BaseActivity<ActivityListsAddBinding, ListsAddViewModel>() {
    override fun initContentView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_lists_add
    }

    override fun initVariableId(): Int {
        return BR.listsAddViewModel
    }

    override fun initParam() {
        super.initParam()
        Locale.setDefault(Locale("en"))
        // 设置背景颜色
        window.statusBarColor = ContextCompat.getColor(this, R.color.c_F6)
        // 设置文字颜色（true = 黑色文字，false = 白色文字）
        val controller = WindowInsetsControllerCompat(window, window.decorView)
        controller.isAppearanceLightStatusBars = true
    }

    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
        val bundle = intent.extras
        bundle?.let {
            viewModel!!.mDeviceId = it.getString("deviceId")
            viewModel!!.isUserType.set(it.getBoolean("userType", false))
            val dataJson = it.getString("data")
            if (!StringUtils.isEmpty(dataJson)) {
                viewModel!!.mListsEntity = Gson().fromJson(dataJson, ListsEntity::class.java)
                viewModel!!.content.set(viewModel!!.mListsEntity!!.name)
                viewModel!!.isShowDelete.set(true)
                if (viewModel!!.items.isNotEmpty()) {
                    viewModel!!.items[viewModel!!.mListsEntity!!.color!!.toInt()].checked.set(true)
                }
                viewModel!!.title.set(getString(R.string.edit_item))
                viewModel!!.btnSaveTxt.set(getString(R.string.update))
                viewModel!!.isHide.set(viewModel!!.mListsEntity!!.isShow == 1)
            } else {
                viewModel!!.isShowDelete.set(false)
                viewModel!!.btnSaveTxt.set(getString(R.string.save))
                viewModel!!.title.set(getString(R.string.add_item))
            }
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        binding!!.etName.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                Utils.instance.hideKeyboard(binding!!.etName)
                return@setOnEditorActionListener true
            }
            return@setOnEditorActionListener false
        }
        viewModel!!.ui.observe(this) {
            if (it == 0) {
                val intent = Intent()
                if (viewModel!!.mListsEntity != null) {
                    val bundle = Bundle()
                    bundle.putString("data", Gson().toJson(viewModel!!.mListsEntity))
                    intent.putExtras(bundle)
                }
                setResult(1005, intent)
                finish()
            } else if (it == 1) {
                setResult(1005)
                finish()
            }
        }
    }
}